<?php
namespace app\common\model;
use think\Model;
class Project extends Model{
    protected $updateTime = false;
    //判断当前用户是否存在进行中的项目
    public function projectIsExists(){
        $where=[
            'uid'=>cookie('uid'),
            'status'=>0
        ];
        $res=$this->where($where)->find();
        if($res){
            return true;
        }else{
            return false;
        }
    }

    //获取当前项目金额
    public function getProjectPrice(){
        $where=[
            'uid'=>cookie('uid'),
            'status'=>0
        ];
        $res=$this->where($where)->find();
        if($res){
            return $res['money'];
        }else{
           return 0;
        }
    }

    //获取用户项目个数
    public function getProjectCount(){
        $where=[
            'uid'=>cookie('uid'),
        ];
        $res=$this->where($where)->count();
        if($res){
           return $res;
        }else{
            return 0;
        }
    }
    //判断活动是否达到签到天数
    public function isFull(){
        $where=[
            'uid'=>cookie('uid'),
            'status'=>0
        ];
        $res=$this->where($where)->find();
        if($res){
            if($res['days']==$res['signed_count']){
                return true;
            }
        }else{
            return false;
        }
    }
    //获取当前进行中的项目
    public function getProject(){
        $where=[
            'uid'=>cookie('uid'),
            'status'=>0,
        ];
        $res=$this->where($where)->find();
            return $res;
    }

    /*
     * 根据打卡id结束当前参与的活动
     *
     * */
    public function overProjectById($id){
       $res= $this->where(array('id'=>$id))->update(array('status'=>1));
        return $res;
    }
    /*
     * 退回当前活动奖金
     */
    public function returnMoney(){
        $user=model('User');
        $where=[
            'uid'=>cookie('uid'),
            'status'=>0
        ];
        $res=$this->where($where)->find();
        if($res){
            $userRes=$user->where(['id'=>$res['uid']])->setInc('balance',$res['money']);
            if(!$userRes){
                return false;
            }else{
                return true;
            }
        }else{
            return false;
        }
    }
    /*
     * 结束本次活动
     *
     * */
    public function closeProject(){
        $where=[
            'uid'=>cookie('uid'),
            'status'=>0
        ];
        $res=$this->where($where)->setField('status',1);
        if($res){
             return true;
        }else{
            return false;
        }
    }

}